﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;

namespace PersonHealth.View
{
    /// <summary>
    /// ReportPage.xaml 的交互逻辑
    /// </summary>
    public partial class ReportPage : Page
    {
        int pageIndex = 1;
        int pageCount = 7;

        public ReportPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MessagePage_Loaded);
        }

        void MessagePage_Loaded(object sender, RoutedEventArgs e)
        {
            pageIndex = 1;
            bindData();
        }

        private void Previous_Click(object sender, MouseButtonEventArgs e)
        {
            if (pageIndex == 1)
                return;
            pageIndex--;
            bindData();
        }

        private void Next_Click(object sender, MouseButtonEventArgs e)
        {
            if (this.DataContext != null && pageIndex == (this.DataContext as HealthReportModel).Pages)
                return;
            pageIndex++;
            bindData();
        }

        private void bindData()
        {
            MeasureUser user = AppData.User;
            IHealthReportService ser = new HealthReportService();

            int msgCount = 0;
            IList<HealthReport> objs = ser.GetUserReports(user.Id, pageIndex, pageCount, out msgCount);
            if (objs != null)
            {
                foreach (var i in objs)
                {
                    if (i.Text.Length > 30)
                    {
                        i.Description = i.Text.Substring(0, 30) + ".........";
                    }
                    else
                    {
                        i.Description = i.Text;
                    }
                }
                MessageList.ItemsSource = objs;

                HealthReportModel model = new HealthReportModel();
                model.TotalMsgs = msgCount;
                model.PageIndex = pageIndex;
                model.PageCount = pageCount;
                model.Pages = (msgCount + pageCount - 1) / pageCount;
                this.DataContext = model;
            }
        }

        private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (MessageList.SelectedItem != null)
            {
                NavigationService.Content = new ReportForm((HealthReport)MessageList.SelectedItem);
            }
        }
    }

    public class HealthReportModel
    {
        public int TotalMsgs { get; set; }

        public int PageCount { get; set; }

        public int PageIndex { get; set; }

        public int Pages { get; set; }
    }
}
